package by.grsu.pereverzev.page;

import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.pereverzev.app.PereverzevWebApplication;
import by.grsu.pereverzev.dao.impl.FlightDao;
import by.grsu.pereverzev.entity.Flight;


public class FlightEditPage extends WebPage {
	public FlightEditPage(final Flight flight) {
		super();
		

		final Form<Flight> form = new Form<Flight>("form", new CompoundPropertyModel<Flight>(flight));
		add(form);

		form.add(new TextField<String>("destStation"));	
		form.add(new TextField<Integer>("cost"));		
		form.add(new DateTextField("dateOfDeparture", "DD.mm.yy HH:MM"));
		
		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();

				FlightDao flightDao = new FlightDao(PereverzevWebApplication.DATA_XML_FOLDER);
				if (flight.getId() == null) {
					System.out.println("save="+flight);
					flightDao.saveNew(flight);
				} else {
					System.out.println("update="+flight);
					flightDao.update(flight);
				}

				setResponsePage(new FlightListPage());
			}
		});
		
		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(new FlightListPage());
			}
		});
		
	}
}
